package com.ruoyi.project.info.appointment.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.info.appointment.domain.AppointmentUpdate;
import com.ruoyi.project.info.patient.domain.Patient;
import com.ruoyi.project.info.patient.mapper.PatientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.info.appointment.mapper.AppointmentMapper;
import com.ruoyi.project.info.appointment.domain.Appointment;
import com.ruoyi.project.info.appointment.service.IAppointmentService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 预约信息Service业务层处理
 *
 * @author wzm
 * @date 2022-09-07
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService
{
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private PatientMapper patientMapper;
    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Appointment selectAppointmentById(Long id)
    {
        return appointmentMapper.selectAppointmentById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param appointment 预约信息
     * @return 预约信息
     */
    @Override
    public List<Appointment> selectAppointmentList(Appointment appointment)
    {
        return appointmentMapper.selectAppointmentList(appointment);
    }

    /**
     * 新增预约信息
     *
     * @param appointment 预约信息
     * @return 结果
     */
    @Override
    public int insertAppointment(Appointment appointment)
    {
        appointment.setCreateTime(DateUtils.getNowDate());
        Patient patient=new Patient();
        patient.setDelFlag("0");
        patient.setName(appointment.getpName());
        patient.setIdcard(appointment.getIdCard());
        patient.setPhone(appointment.getpPhone());
        List<Patient> list = patientMapper.selectPatientList(patient);
        if(list.size()==0){
            patient.setAddress(appointment.getAddress());
            patient.setAge(appointment.getAge());
            patient.setSex(appointment.getSex());
            patient.setdPhone(appointment.getdPhone());
            patient.preInsert();
            patientMapper.insertPatient(patient);
        }
        return appointmentMapper.insertAppointment(appointment);
    }

    /**
     * 修改预约信息
     *
     * @param appointment 预约信息
     * @return 结果
     */
    @Override
    public int updateAppointment(AppointmentUpdate appointment)
    {
        appointment.setUpdateTime(DateUtils.getNowDate());
        return appointmentMapper.updateAppointment(appointment);
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByIds(String ids)
    {
        return appointmentMapper.deleteAppointmentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentById(Long id)
    {
        return appointmentMapper.deleteAppointmentById(id);
    }
}
